<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - 列车车次管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/styles.css">    <style>
        .register-form {
            max-width: 500px;
            margin: 0 auto;
            margin-top: 50px;
            margin-bottom: 50px;
        }
    </style>
</head>
<body class="auth-page">
<div class="container">
    <div class="register-form">
        <div class="card auth-card">
            <div class="card-body">
                <div class="text-center mb-4">
                    <img src="/images/logo.jpeg" alt="Logo" style="height: 60px; width: auto; border-radius: 8px; margin-bottom: 15px;">
                    <h3>列车车次管理系统 - 用户注册</h3>
                </div>

                <div id="registerAlert" class="alert alert-danger d-none" role="alert">
                    注册失败，请检查输入信息
                </div>

                <form id="registerForm">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="username" name="username" required>
                        <div class="form-text">用户名长度为4-20个字符，只能包含字母、数字和下划线</div>
                    </div>

                    <div class="mb-3">
                        <label for="password" class="form-label">密码 <span class="text-danger">*</span></label>
                        <input type="password" class="form-control" id="password" name="password" required>
                        <div class="form-text">密码长度至少为6个字符</div>
                    </div>

                    <div class="mb-3">
                        <label for="confirmPassword" class="form-label">确认密码 <span class="text-danger">*</span></label>
                        <input type="password" class="form-control" id="confirmPassword" required>
                    </div>

                    <div class="mb-3">
                        <label for="name" class="form-label">真实姓名 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="name" name="name" required>
                    </div>

                    <div class="mb-3">
                        <label for="idCard" class="form-label">身份证号 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="idCard" name="idCard" required>
                    </div>

                    <div class="mb-3">
                        <label for="phone" class="form-label">手机号 <span class="text-danger">*</span></label>
                        <input type="tel" class="form-control" id="phone" name="phone" required>
                    </div>

                    <div class="mb-3">
                        <label for="email" class="form-label">邮箱</label>
                        <input type="email" class="form-control" id="email" name="email">
                    </div>

                    <div class="d-grid mb-3">
                        <button type="submit" class="btn btn-primary btn-lg">
                            <i class="fas fa-user-plus me-1"></i>注册
                        </button>
                    </div>
                </form>

                <div class="text-center">
                    <p class="mb-0">已有账号? <a href="/custom-login" class="text-decoration-none">立即登录</a></p>
                    <a href="/" class="text-muted text-decoration-none">
                        <i class="fas fa-arrow-left me-1"></i>返回首页
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    $(document).ready(function() {
        $('#registerForm').on('submit', function(e) {
            e.preventDefault();
            
            // 验证密码匹配
            const password = $('#password').val();
            const confirmPassword = $('#confirmPassword').val();
            
            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }
            
            const formData = {
                username: $('#username').val(),
                password: password,
                name: $('#name').val(),
                idCard: $('#idCard').val(),
                phone: $('#phone').val(),
                email: $('#email').val()
            };
            
            $.ajax({
                url: '/api/users/register',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function() {
                    alert('注册成功！请登录');
                    window.location.href = '/custom-login';
                },
                error: function(xhr) {
                    const errorMessage = xhr.responseText || '注册失败，请检查输入信息';
                    $('#registerAlert').removeClass('d-none').text(errorMessage);
                }
            });
        });
    });
</script>
</body>
</html>